﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using AdaptiveHelpDesk;

namespace Novice
{
    /// <summary>
    /// Captures usage metrics (Predictive Features for all Informative Moments and passes it to service on for processing), does not perform any actual processing.
    /// </summary>
    [WebService(Namespace = "http://soaiui.nmmu.ac.za/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Watcher : System.Web.Services.WebService
    {
        #region Variables -
        #region User Model Combobox Declaration -
        UpdateUserModelComboBox updateUserModelSearchCust;

        UpdateUserModelComboBox updateUserModelServiceName;
        UpdateUserModelComboBox updateUserModelCallType;
        UpdateUserModelComboBox updateUserModelPriority;
        UpdateUserModelComboBox updateUserModelSource;

        UpdateUserModelComboBox updateUserModelCampus;
        UpdateUserModelComboBox updateUserModelContact;

        UpdateUserModelComboBox updateUserModelCause;
        UpdateUserModelComboBox updateUserModelResolved;
        #endregion -

        //initialise Task Model
        UpdateTaskModel updateTModel = new UpdateTaskModel();

        //Initialise get task model
        GetTaskModel gt = new GetTaskModel();

        //Global Methods Class
        GlobalMethods gMethods = new GlobalMethods();

        StoredProcedures sp = new StoredProcedures();
        ArrayList userDetails = new ArrayList();

        String username;

        String userid = "";     // get from task model
        String fname = "";      // write function to pull these based on data from user model
        String lname = "";      // write function to pull these based on data from user model
        String stereotype = ""; // pass as parameter OR pull from user model
      

        #endregion -

        /// <summary>
        /// Receives an array full of aui objects. Each object is processed based on the value of its id.
        /// </summary>
        /// <returns></returns>
        [WebMethod(Description = "Method receives a list of AUI Objects. These hold the Predictive Features for a specific Informative Moment")]
        public void listMetrics(_AUI[] idx)
        {
            if (idx.Length != 0)
            {
                String username = gt.CheckCurrentUser();

                userDetails = (ArrayList)sp.getPassword(username);

                if (userDetails.Count != 0)
                {
                    //string uPassword = userDetails[0].ToString();
                    userid = userDetails[1].ToString();
                    fname = userDetails[2].ToString();
                    lname = userDetails[3].ToString();
                    stereotype = gt.GetStereotype();
                }

                //for (int i = 0; i < idx.Length; i++)
                for (int i = (idx.Length - 1); i > 0;  i--)
                {
                    switch (idx[i].id)
                    {
                        default: break;
                        //step 1
                        case "searchResults":
                            searchResults(idx[i], userid, fname, lname, stereotype);
                            break;

                        //step 2
                        case "serviceName":
                            serviceName(idx[i], userid);
                            break;
                        case "callType":
                            callType(idx[i], userid);
                            break;
                        case "subCallType":
                            subCallType(idx[i], userid);
                            break;
                        case "priority":
                            priority(idx[i], userid);
                            break;
                        case "severity":
                            severity(idx[i], userid);
                            break;
                        case "source":
                            source(idx[i], userid);
                            break;

                        //step 3
                        case "Campus":
                            Campus(idx[i], userid);
                            break;
                        case "Contact":
                            Contact(idx[i], userid);
                            break;

                        //step 4
                        case "Cause":
                            Cause(idx[i], userid);
                            break;
                        case "Solution":
                            Solution(idx[i], userid);
                            break;
                    }
                }
            }
        }
        #region Step 1 -
        public String searchResults(_AUI obj, String userid, String fname, String lname, String stereotype)
        {
            //String TodayDate = DateTime.Today.ToShortDateString().Replace('/', '-');
            //String TodayTime = DateTime.Now.ToLongTimeString();

            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();
          

            // insert user profile
            sp.InsertUserProfile(userid, fname, lname, TodayDate, TodayTime, stereotype);

            // initialise user model
            updateUserModelSearchCust = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMSearchCust", "IMSearchCustomer", "updateUserProfileIMSearchCust", "@IMSearchCust");
            // update user model - note: 0 = yDistance, this is not required as it is calculated on the client side already, so when its needed just skip. Same with the unique items hash table. 
            // change the function so it takes in the int 'uniqueItemsCount'
            gMethods.updateUSERMODELcmb(updateUserModelSearchCust, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);
            //Update Task model here: see Bronwins work
           
            return "success";
        }
        #endregion -

        #region Step 2 -
        public String serviceName(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();
            // Might
            // Initialise user model
            updateUserModelServiceName = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMServiceName", "IMServiceName", "updateUserProfileIMServiceName", "@IMServiceName");
            
            // Update IMServiceName User Model
            if (obj != null)
                gMethods.updateUSERMODELcmb(updateUserModelServiceName, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);

            return "success";
        }
        public String callType(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelCallType = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMCallType", "IMCallType", "updateUserProfileIMCallType", "@IMCallType");
            //Update IMPriority User Model
            if (obj != null)
                gMethods.updateUSERMODELcmb(updateUserModelCallType, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);

            return "success";
        }
        //Not Implemented: Bronwin didn't implement
        public String subCallType(_AUI obj, String userid)
        {
            return "success";
        }
        public String priority(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelPriority = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMPriority", "IMPriority", "updateUserProfileIMPriority", "@IMPriority");
            //Update IMPriority User Model
            if (obj != null)
                gMethods.updateUSERMODELcmb(updateUserModelPriority, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);

            return "success";
        }
        //Not Implemented: Bronwin didn't implement
        public String severity(_AUI obj, String userid)
        {
            return "success";
        }
        public String source(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelSource = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMSource", "IMSource", "updateUserProfileIMSource", "@IMSource");
            
            //Update IMSource User Model
            if (obj != null)
                gMethods.updateUSERMODELcmb(updateUserModelSource, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);

            return "success";
        }
        #endregion -

        #region Step 3 -
        public String Campus(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelCampus = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMCampus", "IMCampus", "updateUserProfileIMCampus", "@IMCampus");
            //Update IMCampus User Model
            gMethods.updateUSERMODELcmb(updateUserModelCampus, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);

            return "success";
        }
        public String Contact(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelContact = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMContact", "IMContact", "updateUserProfileIMContact", "@IMContact");
            //Update IMContact User Model
            gMethods.updateUSERMODELcmb(updateUserModelContact, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);
            
            return "success";
        }
        #endregion -

        #region Step 4 -
        public String Cause(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelCause = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMCause", "IMCause", "updateUserProfileIMCause", "@IMCause");
            //Update IMCause User Model
            gMethods.updateUSERMODELcmb(updateUserModelCause, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);
                        
            return "success";
        }
        public String Solution(_AUI obj, String userid)
        {
            String TodayDate = gt.GetLogginDate();
            String TodayTime = gt.GetLogginTime();

            //initialise user model
            updateUserModelResolved = new UpdateUserModelComboBox(userid, TodayDate, TodayTime, "getUserProfileIMResolved", "IMResolved", "updateUserProfileIMResolved", "@IMResolved");
            //Update IMResolved User Model
            gMethods.updateUSERMODELcmb(updateUserModelResolved, obj.totalTime, obj.yMouseV, obj.yMouseA, obj.dwellTime, obj.avgDwellTime, obj.nrVisitedItems, obj.uniqueItemCount, obj.selectionTime, obj.KLMRatio, obj.KLMDifference);

           return "success";
        }
        #endregion -
    }
}
